package com.akk.platform.manage;

import com.akk.module.avs.entity.AvsApplyCard;
import com.akk.module.avs.model.request.AvsCardRequest;
import com.akk.module.avs.service.AvsApplyCardService;
import com.akk.module.upload.service.PictureService;
import com.akk.utils.PageUtils;
import com.akk.utils.Query;
import com.akk.utils.RestResponse;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.commons.lang.StringEscapeUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/platform/manage/card")
public class ApplyCardAction {


    @Autowired
    private AvsApplyCardService cardService;

    @Autowired
    private PictureService pictureServices;

    @RequestMapping(value = "/list")
    @RequiresPermissions("sys:apply:card:list")
    public RestResponse list(@Valid Query query, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage(),null);
        PageHelper.startPage(query.getPage(),query.getLimit());
        List<AvsApplyCard> list = cardService.list();
        return RestResponse.build(PageUtils.buildPage(list,(int) new PageInfo<>(list).getTotal(),query));
    }

    @RequestMapping(value = "/save",method = RequestMethod.POST)
    @RequiresPermissions("sys:apply:card:save")
    public RestResponse save(@Valid AvsCardRequest request, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        AvsApplyCard cardEntity = new AvsApplyCard();
        cardEntity.setCardUrl(StringEscapeUtils.unescapeHtml(request.getCardUrl()));
        cardEntity.setCardDiscount(request.getCardDiscount());
        cardEntity.setCardDesc(request.getCardDesc());
        cardEntity.setCardBankName(request.getCardBankName());
        cardEntity.setCardIndex(request.getCardIndex());
        if (request.getCardBanner() != null){
            Map<String,Object> result = pictureServices.uploadPicture(request.getCardBanner());
            if (result.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            cardEntity.setCardBanner(result.get("url").toString());
        }else {
            return RestResponse.build("1","请上传文件");
        }
        cardService.save(cardEntity);
        return RestResponse.build("success");
//        writeToResponse(response,new Gson().toJson(R.ok()));
    }

    @RequestMapping(value = "/update",method = RequestMethod.POST)
    @RequiresPermissions("sys:apply:card:update")
    public RestResponse update(@Valid AvsCardRequest request, BindingResult bindingResult){
        if (bindingResult.hasErrors())
            return RestResponse.build("1",bindingResult.getFieldError().getDefaultMessage());
        if (request.getCardId() == null || request.getCardId() == 0){
            return RestResponse.build("1","信息不存在");
        }
        AvsApplyCard cardEntity = cardService.queryById(request.getCardId());
        if (cardEntity == null){
            return RestResponse.build("1","信息不存在");
        }
        if (request.getCardBanner() != null){
            Map<String,Object> result1 = pictureServices.uploadPicture(request.getCardBanner());
            if (result1.get("status").toString().equals("1")){
                return RestResponse.build("1","文件上传失败");
            }
            cardEntity.setCardBanner(result1.get("url").toString());
        }
        cardEntity.setCardBankName(request.getCardBankName());
        cardEntity.setCardDesc(request.getCardDesc());
        cardEntity.setCardIndex(request.getCardIndex());
        cardEntity.setCardDiscount(request.getCardDiscount());
        cardEntity.setCardStatus(request.getCardStatus());
        cardEntity.setCardUrl(StringEscapeUtils.unescapeHtml(request.getCardUrl()));
        cardService.update(cardEntity);
        return RestResponse.build("success");
    }

    @RequestMapping(value = "/delete/{id}")
    @RequiresPermissions("sys:apply:card:delete")
    public RestResponse delete(@PathVariable Long id){
        cardService.delete(id);
        return RestResponse.build("success");
    }

}
